//
//  RootViewController.m
//  AskApp
//
//  Created by Gordon Liu on 11-8-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "HomePage.h"
#import "AskPage.h"
#import "VotePage.h"
#import "ResultsPage.h"
#import "MePage.h"

@implementation RootViewController

@synthesize tabBar;
@synthesize homePage;
@synthesize askPage;
@synthesize votePage;
@synthesize resultsPage;
@synthesize mePage;

- (void)viewDidLoad {
	
	[self showHomePage];
	
	//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleQueueLengthChanged:) name:@"Queue Length Changed" object:nil];
	
	//[self hideInfoButton];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
	if(item.tag == 0){
		[self showHomePage];
	}
	else if(item.tag == 1){
		[self showAskPage];
	}
	else if(item.tag == 2){
		[self showVotePage];
	}
    else if(item.tag == 3){
		[self showResultsPage];
	}
    else if(item.tag == 4){
		[self showMePage];
	}
    
}

- (void)selectTabByTag:(NSInteger) tag {
	NSArray *items = [tabBar items];
	for (NSInteger i = 0; i < [items count]; ++i) {
		if([[items objectAtIndex:i] tag] == tag){
			tabBar.selectedItem = [items objectAtIndex:i];
			break;
		}
	}
}

- (void)hideTabBar {
	[UIView beginAnimations:nil context:NULL];
	tabBar.frame = CGRectMake(0.0, 460.0, tabBar.frame.size.width, tabBar.frame.size.height);
	[UIView commitAnimations];
}

- (void)showTabBar {
	[UIView beginAnimations:nil context:NULL];
	tabBar.frame = CGRectMake(0.0, 411.0, tabBar.frame.size.width, tabBar.frame.size.height);
	[UIView commitAnimations];
}

- (void)loadHomePage {
	HomePage *page = [[HomePage alloc] initWithNibName:@"HomePage" bundle:nil];
	self.homePage = page;
	homePage.rootViewController = self;
	[page release];
}

- (void)loadAskPage {
	AskPage *page = [[AskPage alloc] initWithNibName:@"AskPage" bundle:nil];
	self.askPage = page;
	askPage.rootViewController = self;
	[page release];
}

- (void)loadVotePage {
	VotePage *page = [[VotePage alloc] initWithNibName:@"VotePage" bundle:nil];
	self.votePage = page;
	votePage.rootViewController = self;
	[page release];
}

- (void)loadResultsPage {
	ResultsPage *page = [[ResultsPage alloc] initWithNibName:@"ResultsPage" bundle:nil];
	self.resultsPage = page;
	resultsPage.rootViewController = self;
	[page release];
}

- (void)loadMePage {
	MePage *page = [[MePage alloc] initWithNibName:@"MePage" bundle:nil];
	self.mePage = page;
	mePage.rootViewController = self;
	[page release];
}

- (void)hidePage : (UIViewController*) page{
	if(page != nil){
		[page viewWillDisappear:YES];
		[page.view removeFromSuperview];
		[page viewDidDisappear:YES];
	}
}

- (void)showHomePage{
	if(homePage == nil){
		[self loadHomePage];
	}
    
	//[self hidePage: homePage];
	[self hidePage: askPage];
    [self hidePage: votePage];
    [self hidePage: resultsPage];
    [self hidePage: mePage];
	[homePage viewWillAppear:YES];
	[self.view insertSubview:homePage.view belowSubview:tabBar];
	[homePage viewDidAppear:YES];
	[self selectTabByTag:0];
}

- (void)showAskPage{
	if(askPage == nil){
		[self loadAskPage];
	}
    
	[self hidePage: homePage];
	//[self hidePage: askPage];
    [self hidePage: votePage];
    [self hidePage: resultsPage];
    [self hidePage: mePage];
	[askPage viewWillAppear:YES];
	[self.view insertSubview:askPage.view belowSubview:tabBar];
	[askPage viewDidAppear:YES];
	[self selectTabByTag:1];
}
- (void)showVotePage{
	if(votePage == nil){
		[self loadVotePage];
	}
    
	[self hidePage: homePage];
	[self hidePage: askPage];
    //[self hidePage: votePage];
    [self hidePage: resultsPage];
    [self hidePage: mePage];
	[votePage viewWillAppear:YES];
	[self.view insertSubview:votePage.view belowSubview:tabBar];
	[votePage viewDidAppear:YES];
	[self selectTabByTag:2];
}
- (void)showResultsPage{
	if(resultsPage == nil){
		[self loadResultsPage];
	}
    
	[self hidePage: homePage];
	[self hidePage: askPage];
    [self hidePage: votePage];
    //[self hidePage: resultsPage];
    [self hidePage: mePage];
	[resultsPage viewWillAppear:YES];
	[self.view insertSubview:resultsPage.view belowSubview:tabBar];
	[resultsPage viewDidAppear:YES];
	[self selectTabByTag:3];
}
- (void)showMePage{
	if(mePage == nil){
		[self loadMePage];
	}
    
	[self hidePage: homePage];
	[self hidePage: askPage];
    [self hidePage: votePage];
    [self hidePage: resultsPage];
    //[self hidePage: mePage];
	[mePage viewWillAppear:YES];
	[self.view insertSubview:mePage.view belowSubview:tabBar];
	[mePage viewDidAppear:YES];
	[self selectTabByTag:4];
}

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}

- (void)dealloc {
    [tabBar release];
    [homePage release];
    [askPage release];
    [votePage release];
    [resultsPage release];
    [mePage release];

    [super dealloc];
}


@end
